﻿var HH = 23;
var MM = 59;
var SS = 59;
var mode = {};
var LOOP;

window.loopInit = function (id, sign) {
	LOOP = true;
	change(id, sign);
}
window.loopBreak = function () {
	LOOP = false;
}
window.getTag = function (id, tagName, str, attr) {
	var tagArray = document.getElementById(id).getElementsByTagName(tagName);
	for (i = 0; i < tagArray.length; i++) {
		if (eval("tagArray[i]." + attr) == str) {
			return tagArray[i];
		}
	}
}
window.setMode = function (id, hms) {
	var selectInputPre = getTag(id, 'input', mode[id + "mode"], 'name');
	var selectInput = getTag(id, 'input', hms, 'name');
	selectInputPre.style.backgroundColor = "#FFFFFF";
	mode[id + "mode"] = hms;
	selectInput.style.backgroundColor = "#7FFFD4";
}
window.change = function (id, sign) {
	if (LOOP) {
		var num = getTag(id, 'input', mode[id + "mode"], 'name').value - 0;
		var changeNum = eval(num + sign + 1) + "";
		if (changeNum >= 0 & changeNum <= eval(mode[id + "mode"])) {
			changeNum = complete(changeNum);
			getTag(id, 'input', mode[id + "mode"], 'name').value = changeNum;
		}
		setTimeout("change('" + id + "','" + sign + "')", 200);
	}
}
window.complete = function (num) {
	while (!(num.length == 2)) {
		num = "0" + num;
	}
	return num;
}
window.returnTimer=function(id) {
	var strHH = getTag(id, 'input', 'HH', 'name').value;
	var strMM = getTag(id, 'input', 'MM', 'name').value;
	var strSS = getTag(id, 'input', 'SS', 'name').value;
	return strHH + ':' + strMM + ':' + strSS;
}
window.checkNum=function(id, sign, value) {
	if (value < 10) {
		value = complete(value);
		getTag(id, 'input', sign, 'name').value = value;
	} else {
		if (value > eval(sign)) {
			getTag(id, 'input', sign, 'name').value = eval(sign);
		}
	}
}
 window.keyDown=function(id, key, onFocusObject){
	var nextObject;
	if (onFocusObject.name == 'HH') {
		nextObject = 'MM';
	} else if (onFocusObject.name == 'MM') {
		nextObject = 'SS';
	} else if (onFocusObject.name == 'SS') {
		nextObject = 'HH';
	}
	if (!((key >= 48 && key <= 57) || (key >= 96 && key <= 105) || (key == 8) || (key == 46) || (key >= 37 && key <= 40))) {
		event.returnValue = false;
	}
	if (key == 37 || key == 39) {
		getTag(id, 'input', nextObject, 'name').focus();
	}
	if (key == 38) {
		loopInit(id, '+');
	}
	if (key == 40) {
		loopInit(id, '-');
	}
}

//时间，如果是onlur，找选择器时可以直接“this”,特指当前对象
window.blurTime = function (id) {
	if ($(id).val() == "") {
		$(id).val("00");

	} 
	var reg=/[^\d]/g;//判断是否是数字
	if(reg.test($(id).val())){
	//	alert("进来");
		$(id).val("00");
	}

}


window.blurHH = function(h) {
	//时间格式，小时
	var reg = /^(2[0-3]|[0-1]\d)$/;
	if (!reg.test($(h).val())) {
		$(h).val(new Date().getHours);
	}
}

window.blurMM = function(m) {
	//时间格式，分
	var reg = /^[0-5]\d|60$/;
	if (!reg.test($(m).val())) {
		$(m).val(new Date().getMinutes);
	}

}


 

window.keyUp = function (key) {
	if (key == 38 || 40) {
		loopBreak();
	}
}
//等于1，时间段默认禁用；等于2，选择秒的时间，不填默认分
window.showTimer = function (tempId,i) {
	var d=new Date();
	var getHours=d.getHours();
	var getMinutes=d.getMinutes();
	
	if(getHours<10){
		getHours='0'+getHours;
	}
	if(getMinutes<10){
		getMinutes='0'+getMinutes;
	}
	
	mode[tempId + "mode"] = "HH";
	if(i!=2){
	var timerConent = '<table cellpadding="0" cellspacing="0" style="border:#949495 1px solid;table-layout : fixed" >' +																																																																																																																																																																																																																																			
		'<tr style="width:60px;height:20px">' +
		'<td width="40" style="border:0px;"><input type="text"  maxlength="2" style="border:0;background:transparent;width:15px;" name="HH" value="'+getHours+'" onchange="checkNum(' + "'" + tempId + "'," + "'HH'," + 'this.value)" onkeydown="keyDown(' + "'" + tempId + "'" + ',event.keyCode,this)" onkeyup="keyUp(event.keyCode)" onfocus="setMode(' + "'" + tempId + "','HH'" + ')" onblur="blurTime(this),blurHH(this)"  /><input type="text"  style="border:0;background:transparent;width:6px;" readOnly=true  value=":"/><input type="text"  maxlength="2" style="border:0;background:transparent;width:15px" name="MM" value="'+getMinutes+'" onchange="checkNum(' + "'" + tempId + "'," + "'MM'," + 'this.value)" onkeydown="keyDown(' + "'" + tempId + "'" + ',event.keyCode,this)" onkeyup="keyUp(event.keyCode)" onfocus="setMode(' + "'" + tempId + "','MM'" + ')" onblur="blurTime(this),blurMM(this)" /></td>' +
		'<td width="20" style="border:0px;"><input type="button" hidefocus="true" style="display: block;border-width:2px;background:url(static/img/times.png);width:20px;height:10px;" onmouseup="loopBreak()" onmousedown="loopInit(' + "'" + tempId + "','+'" + ')"/><input type="button" hidefocus="true" style="border-width:2px ;background:url(static/img/timex.png);width:20px;height:10px;display: block;" onmouseup="loopBreak()" onmousedown="loopInit(' + "'" + tempId + "','-'" + ')"/></td>' +
		'</tr>' +
		'</table>';
}else{
		var timerConent = '<table cellpadding="0" cellspacing="0" style="border:#949495 1px solid;table-layout : fixed" >' +
			'<tr style="width:60px;height:20px">' +
			'<td width="60" style="border:0px;display:block;"><input type="text"  maxlength="2" style="border:0;background:transparent;width:15px;" name="HH" value="00" onchange="checkNum(' + "'" + tempId + "'," + "'HH'," + 'this.value)" onkeydown="keyDown(' + "'" + tempId + "'" + ',event.keyCode,this)" onkeyup="keyUp(event.keyCode)" onfocus="setMode(' + "'" + tempId + "','HH'" + ')"/><input type="text"  style="border:0;background:transparent;width:6px;" readOnly=true  value=":"/><input type="text"  maxlength="2" style="border:0;background:transparent;width:15px" name="MM" value="00" onchange="checkNum(' + "'" + tempId + "'," + "'MM'," + 'this.value)" onkeydown="keyDown(' + "'" + tempId + "'" + ',event.keyCode,this)" onkeyup="keyUp(event.keyCode)" onfocus="setMode(' + "'" + tempId + "','MM'" + ')"/><input type="text"  style="border:0;background:transparent;width:6px"  readOnly=true value=":"/><input type="text"  maxlength="2" style="border:0;background:transparent;width:15px" name="SS" value="00" onchange="checkNum(' + "'" + tempId + "'," + "'SS'," + 'this.value)" onkeydown="keyDown(' + "'" + tempId + "'" + ',event.keyCode,this)" onkeyup="keyUp(event.keyCode)" onfocus="setMode(' + "'" + tempId + "','SS'" + ')"/></td>' +
			'<td width="20" style="border:0px;"><input type="button" hidefocus="true" style="display: block;border-width:2px;background:url(static/img/times.png);width:20px;height:10px;" onmouseup="loopBreak()" onmousedown="loopInit(' + "'" + tempId + "','+'" + ')"/><input type="button" hidefocus="true" style="border-width:2px ;background:url(static/img/timex.png);width:20px;height:10px;display: block;" onmouseup="loopBreak()" onmousedown="loopInit(' + "'" + tempId + "','-'" + ')"/></td>' +
			'</tr>' +
			'</table>';
}
		document.getElementById(tempId).innerHTML = timerConent;
	
		if(i==1){
		setTimeout(function () {
			$("#"+ tempId).find("input").attr("disabled", true);
			$("#"+ tempId).css("color", "gainsboro");
		}, 500);
	
	}


}
window.onload = function () {
	
	 
}





